﻿using Contrib.BSNavigation.Models;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;

namespace Contrib.BSNavigation.Drivers
{
    [UsedImplicitly]
    public class BSLinkPartDriver : ContentPartDriver<BSLinkPart>
    {

        // Best practice: always specify a Prefix to avoid potential input name conflicts
        protected override string Prefix
        {
            get { return "BSLink"; }
        }

        protected override DriverResult Editor(BSLinkPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_BSLink_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/BSLink", Model: part, Prefix: Prefix));
        }

        protected override DriverResult Editor(BSLinkPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }

        // Add support for exporting our part
        protected override void Exporting(BSLinkPart part, ExportContentContext context)
        {
            context.Element(part.PartDefinition.Name).SetAttributeValue("CustomAttributeName", part.CustomAttributeName);
            context.Element(part.PartDefinition.Name).SetAttributeValue("CustomAttributeValue", part.CustomAttributeValue);
        }

        // Add support for importing our part
        protected override void Importing(BSLinkPart part, ImportContentContext context)
        {
            context.ImportAttribute(part.PartDefinition.Name, "CustomAttributeName", x => part.CustomAttributeName = x);
            context.ImportAttribute(part.PartDefinition.Name, "CustomAttributeValue", x => part.CustomAttributeValue = x);
        }

    }
}